import os
from tqdm import tqdm
from .base import Tool

def execute(params: dict) -> str:
    """执行write_file工具"""
    try:
        path = params.get("path", "")
        content = params.get("content", "")
        
        if not path:
            return "错误：未提供文件路径"
            
        if not content:
            return "错误：未提供写入内容"
            
        with open(path, "w", encoding="utf-8") as f:
            f.write(content)
            
        return f"文件已写入: {path}"
        
    except Exception as e:
        return f"写入文件失败: {str(e)}"

TOOL = Tool(
    name="write_file",
    description="写入内容到指定文件",
    parameters={
        "path": {
            "type": "string",
            "description": "要写入的文件路径",
            "required": True
        },
        "content": {
            "type": "string",
            "description": "要写入的内容",
            "required": True
        }
    },
    example='''<write_file>
<path>example.txt</path>
<content>Hello World!</content>
</write_file>''',
    execute_func=execute
)